package com.bidevalution.business.vote;

import com.alibaba.druid.util.StringUtils;
import com.bidevalution.business.impl.BeExpertVoteServiceImpl;
import com.bidevalution.business.service.BeEvalutionOutcomeService;
import com.bidevalution.business.service.VoteUnitNumService;
import com.bidevalution.dao.entity.BeExpertVoteCallBack;
import com.bidevalution.dao.entity.VoteParams;
import com.qtp.core.model.Response;
import com.qtp.core.util.GsonUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 更新统一后的数据
 */
@Log4j2
@Component
public class ConsistencyCheckCallback extends BeExpertVoteServiceImpl {
    @Autowired
    private BeEvalutionOutcomeService beEvalutionOutcomeService;
    @Autowired
    private VoteUnitNumService voteUnitNumService;

    @Override
    public Response toUpdateTheVote(List<BeExpertVoteCallBack> voteCallBackList) {
        String sectionId = null;
        String sectionStepId = null;
        for(BeExpertVoteCallBack beExpertVoteCallBack : voteCallBackList) {
            sectionId = beExpertVoteCallBack.getSectionId();
            sectionStepId = beExpertVoteCallBack.getSectionStepId();
            String voteParamsStr = beExpertVoteCallBack.getVoteParams();
            if (!StringUtils.isEmpty(voteParamsStr)) {
                VoteParams voteParams = GsonUtil.parseJsonWithGson(voteParamsStr, VoteParams.class);
                voteParams.setResult(beExpertVoteCallBack.getVoteResule());
                beEvalutionOutcomeService.updateResult(voteParams);
            }
        }

        voteUnitNumService.checkInitiateVote(sectionId, sectionStepId,3,0);
        return Response.ok();
    }
}
